import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Auth} from '../_models/auth';
import {Observable} from 'rxjs/Observable';

const BASE_URL = 'http://qcloud.housir.vip:8080';
const API_URL = BASE_URL + '/api';

@Injectable()
export class AuthService {

  private _jwtToken: string;

  constructor(private httpClient: HttpClient) {
    this._jwtToken = localStorage.getItem('jwt_token');
  }

  doLogin(auth: Auth): Observable<Object> {
    return this.httpClient.post(API_URL + '/login', JSON.stringify(auth));
  }

  doReset(auth: Auth): Observable<Object> {
    return this.httpClient.post(API_URL + '/reset', JSON.stringify(auth));
  }

  doRegister(auth: Auth): Observable<Object> {
    return this.httpClient.post(API_URL + '/reg', JSON.stringify(auth));
  }

  doLogout(): void {
    this.httpClient.get(API_URL + '/jwt/logout');
    this.jwtToken = '';
  }

  get jwtToken(): string {
    return this._jwtToken;
  }

  set jwtToken(value: string) {
    localStorage.setItem('jwt_token', value);
    this._jwtToken = value;
  }
}
